home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / VMM_src.lha / VMM / reset_handler.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-16  |  4.4 KB  |  150 lines

  1. /**************************************************************************
  2.  * This is a reentrant generic reset handler module. It provides routines *
  3.  * to install and remove a function to be called upon keyboard reset.     *
  4.  * The available functions are:                                           *
  5.  *   1. void *InstallResetHandler (void (*func) (void), LONG priority);   *
  6.  *        This installs the function 'func', which is called in the event *
  7.  *        of a reset. If this function is not successful, it returns NULL,*
  8.  *        otherwise it returns a pointer to be given to RemoveResetHandler*
  9.  *        when deinstalling the handler.                                  *
  10.  *                                                                        *
  11.  *   2. void RemoveResetHandler (void* ResetHandlerParams);               *
  12.  *        Removes a previously install reset handler. This function is    *
  13.  *        safe to call even if InstallResetHandler failed.                *
  14.  *                                                                        *
  15.  *   3. void ResetHandlerDone (void *ResetHandlerParams);                 *
  16.  *        Informs the system that it's OK to reset the system as far as   *
  17.  *        this resethandler is concerned.                                 *
  18.  **************************************************************************/
  19.  
  20. #include <exec/types.h>
  21. #include <exec/memory.h>
  22. #include <exec/io.h>
  23. #include <exec/interrupts.h>
  24. #include <devices/keyboard.h>
  25. #include <clib/exec_protos.h>
  26. #include <clib/alib_protos.h>
  27.  
  28. struct ResetHandlerData
  29.   {
  30.   struct MsgPort *ResetPort;
  31.   struct Interrupt *ResetInt;
  32.   struct IOStdReq *ResetReq;
  33.   };
  34.  
  35. void *InstallResetHandler (void (*func) (void), LONG priority)
  36.  
  37. {
  38. struct Interrupt *ResetInt;
  39. struct MsgPort *ResetPort;
  40. struct IOStdReq *ResetReq;
  41. struct ResetHandlerData *rhd;
  42.  
  43. if ((rhd = AllocMem (sizeof (struct ResetHandlerData), MEMF_PUBLIC)) == NULL)
  44.   return (NULL);
  45.  
  46. if ((ResetInt = AllocMem (sizeof (struct Interrupt), MEMF_PUBLIC)) == NULL)
  47.   {
  48.   FreeMem (rhd, sizeof (struct ResetHandlerData));
  49.   return (NULL);
  50.   }
  51.  
  52. rhd->ResetInt = ResetInt;
  53.  
  54. if ((ResetPort = CreateMsgPort ()) == NULL)
  55.   {
  56.   FreeMem (ResetInt, sizeof (struct Interrupt));
  57.   FreeMem (rhd, sizeof (struct ResetHandlerData));
  58.   return (NULL);
  59.   }
  60.  
  61. rhd->ResetPort = ResetPort;
  62.  
  63. if ((ResetReq = CreateIORequest (ResetPort, sizeof (struct IOStdReq))) == NULL)
  64.   {
  65.   DeleteMsgPort (ResetPort);
  66.   FreeMem (ResetInt, sizeof (struct Interrupt));
  67.   FreeMem (rhd, sizeof (struct ResetHandlerData));
  68.   return (NULL);
  69.   }
  70.  
  71. rhd->ResetReq = ResetReq;
  72.  
  73. if (OpenDevice ("keyboard.device", 0L, (struct IORequest*)ResetReq, 0L) != NULL)
  74.   {
  75.   DeleteIORequest (ResetReq);
  76.   DeleteMsgPort (ResetPort);
  77.   FreeMem (ResetInt, sizeof (struct Interrupt));
  78.   FreeMem (rhd, sizeof (struct ResetHandlerData));
  79.   return (NULL);
  80.   }
  81.  
  82. /* All needed resources allocated now */
  83. ResetInt->is_Node.ln_Pri = priority;
  84. ResetInt->is_Code = func;
  85. ResetReq->io_Data = ResetInt;
  86. ResetReq->io_Command = KBD_ADDRESETHANDLER;
  87. DoIO ((struct IORequest*)ResetReq);
  88. if (ResetReq->io_Error != 0)
  89.   {
  90.   CloseDevice ((struct IORequest*)ResetReq);
  91.   DeleteIORequest (ResetReq);
  92.   DeleteMsgPort (ResetPort);
  93.   FreeMem (ResetInt, sizeof (struct Interrupt));
  94.   FreeMem (rhd, sizeof (struct ResetHandlerData));
  95.   return (NULL);
  96.   }
  97.  
  98. return (rhd);
  99. }
  100.  
  101. /************************************************************************/
  102.  
  103. void RemoveResetHandler (void* ResetHandlerParams)
  104.  
  105. {
  106. struct ResetHandlerData *rhd;
  107. struct Interrupt *ResetInt;
  108. struct MsgPort *ResetPort;
  109. struct IOStdReq *ResetReq;
  110.  
  111. if (ResetHandlerParams == NULL)
  112.   return;
  113.  
  114. rhd = (struct ResetHandlerData*)ResetHandlerParams;
  115. ResetInt = rhd->ResetInt;
  116. ResetPort = rhd->ResetPort;
  117. ResetReq = rhd->ResetReq;
  118.  
  119. ResetReq->io_Command = KBD_REMRESETHANDLER;
  120. ResetReq->io_Data = ResetInt;
  121. DoIO ((struct IORequest*)ResetReq);
  122. CloseDevice ((struct IORequest*)ResetReq);
  123. DeleteIORequest (ResetReq);
  124. DeleteMsgPort (ResetPort);
  125. FreeMem (ResetInt, sizeof (struct Interrupt));
  126. FreeMem (rhd, sizeof (struct ResetHandlerData));
  127. }
  128.  
  129. /************************************************************************/
  130.  
  131. void ResetHandlerDone (void *ResetHandlerParams)
  132.  
  133. {
  134. struct ResetHandlerData *rhd;
  135. struct Interrupt *ResetInt;
  136. struct IOStdReq *ResetReq;
  137.  
  138. if (ResetHandlerParams == NULL)
  139.   return;
  140.  
  141. rhd = (struct ResetHandlerData*)ResetHandlerParams;
  142. ResetInt = rhd->ResetInt;
  143. ResetReq = rhd->ResetReq;
  144.  
  145.  
  146. ResetReq->io_Command = KBD_RESETHANDLERDONE;
  147. ResetReq->io_Data = ResetInt;
  148. DoIO ((struct IORequest*)ResetReq);
  149. }
  150.